package com.andeveloper.kyodai;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class MainMenuScreen implements Screen {
	private Image bg;
	private TextButton btNormalPlay;
	private TextButton btCrazyPlay;
	private Kyodai game;
	private Label label;
	private Stage stage;

	public MainMenuScreen(Kyodai game) {
		this.game = game;
		NinePatch localNinePatch = new NinePatch(Assets.orangebg, 10, 21, 21,
				10);
		BitmapFont localBitmapFont1 = new BitmapFont(
				Gdx.files.internal("font/kyodai.fnt"),
				Gdx.files.internal("font/kyodai.png"), false);
		TextButton.TextButtonStyle localTextButtonStyle = new TextButton.TextButtonStyle(new NinePatchDrawable(localNinePatch), new NinePatchDrawable(localNinePatch), new NinePatchDrawable(localNinePatch), localBitmapFont1);
		this.btNormalPlay = new TextButton("普通模式", localTextButtonStyle);
		this.btNormalPlay.setWidth(240.0F);
		this.btNormalPlay.setHeight(80.0F);
		this.btNormalPlay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				click(MainMenuScreen.this.btNormalPlay);
			}
		});

		this.btCrazyPlay = new TextButton("挑战模式", localTextButtonStyle);
		this.btCrazyPlay.setWidth(240.0F);
		this.btCrazyPlay.setHeight(80.0F);
		this.btCrazyPlay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				click(MainMenuScreen.this.btCrazyPlay);
			}
		});

		this.bg = new Image(new NinePatch(Assets.bg, 10, 10, 20, 20));
		this.bg.setHeight(GameConfig.SCREEN_HEIGHT);
		this.bg.setWidth(GameConfig.SCREEN_WIDTH);
		BitmapFont localBitmapFont2 = new BitmapFont(
				Gdx.files.internal("font/kyodai.fnt"),
				Gdx.files.internal("font/kyodai.png"), false);
		this.label = new Label("连连看", new Label.LabelStyle(localBitmapFont2,
				new Color(0.0F, 1.0F, 1.0F, 1.0F)));
		this.label.setFontScale(2.0F);
		this.label.setX(GameConfig.SCREEN_WIDTH / 2 - this.label.getWidth() / 2.0F);
		this.label.setY((50.0F + (this.btNormalPlay.getY() + this.btNormalPlay.getHeight())));

		Table table = new Table();
		this.label.setY((50.0F + (this.btNormalPlay.getY() + this.btNormalPlay.getHeight())));
		table.add(label);
		table.row();
		table.add(btNormalPlay).width(240.0F);
		table.row();
		table.add(btCrazyPlay).width(240.0F);
		table.setPosition(GameConfig.SCREEN_WIDTH / 2, GameConfig.SCREEN_HEIGHT / 2.0F);

		this.stage = new Stage(new StretchViewport(GameConfig.SCREEN_WIDTH, GameConfig.SCREEN_HEIGHT));
		this.stage.addActor(this.bg);
		this.stage.addActor(table);
	}

	public void click(Actor paramActor) {
		if (paramActor == this.btNormalPlay) {
			GameConfig.GAME_MODE = GameConfig.MODE_NORMAL;
			getGame().setScreen(getGame().getSelectLevelScreen());
		} else if (paramActor == this.btCrazyPlay) {
			GameConfig.GAME_MODE = GameConfig.MODE_CRAZY;
			GameConfig.crazyModeTime = 0;
			GameScreen localGameScreen = getGame().getGameScreen();
			localGameScreen.resetGame(0);
			getGame().setScreen(localGameScreen);
		}
		if (GameConfig.sound)
			Assets.sel.play();
	}

	public void dispose() {
		this.stage.dispose();
	}

	public void hide() {
	}

	public void pause() {
	}

	public void render(float paramFloat) {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		this.stage.act(Gdx.graphics.getDeltaTime());
		this.stage.draw();
	}

	public void resize(int paramInt1, int paramInt2) {
	}

	public void resume() {
	}

	public void show() {
//		game.getApp().setAdViewVisibility(true);
		Gdx.input.setInputProcessor(this.stage);
	}

	public Kyodai getGame() {
		return game;
	}
}
